//
//  TimeExt.swift
//  covermedia
//
//  Created by travel on 16/7/18.
//  Copyright © 2016年 covermedia. All rights reserved.
//

import Foundation



private let kDateFormatter = DateFormatter()
private let kTimestampMark = ceil(Double(Date.init().timeIntervalSince1970 * 10))


public enum CMTimeFormatType {
    case brief, full, qface
}

public extension CMExt where DT == Int {
    /// 阅读数量
    public var reviewCount: String {
        if v < 10000 {
            return "\(v)"
        } else if v < 1000000 && v >= 10000 {
            return String.init(format: "%.1f万", Double(v)/10000)
        } else if v < 100000000 && v >= 1000000 {
            return String.init(format: "%.0f万", Double(v)/10000)
        } else {
            return String.init(format: "%.1f亿", Double(v)/100000000)
        }
    }
    /// 列表评论数量
    public var replyCount: String {
        if v <= 999 {
            return "\(v)"
        }else {
            return String.init(format: "%.1fk", Double(v)/1000)
        }
    }
}

public extension CMExt where DT == Double {
    
    /// 返回时间戳（秒为单位）
    public var timestamp: Double {
        return v > kTimestampMark ? v / 1000 : v
    }
    
    /// 返回可阅读的字符串
    ///
    /// - Parameter type: 类型
    /// - Returns: 字符串
    public func humanTime(type: CMTimeFormatType = .brief) -> String {
        let timestampInSeconds = self.timestamp
        switch type {
        case .brief:
            let now = Date.init()
            let interval = now.timeIntervalSince1970 - timestampInSeconds
            var readbleString:String!
            if interval < 3600 * 24 {
                if interval >= 3600 {
                    readbleString = "\(Int(interval/3600))小时前"
                } else if interval > 60 {
                    readbleString = "\(Int(interval/60))分钟前"
                } else {
                    readbleString = "刚刚"
                }
            } else {
                let date = Date.init(timeIntervalSince1970: timestampInSeconds)
                let dateFormater = kDateFormatter
                let ca = Calendar.init(identifier: Calendar.Identifier.gregorian)
                if ca.component(.year, from: now) != ca.component(.year, from: date) {
                    dateFormater.dateFormat = "yyyy-MM-dd HH:mm"
                } else {
                    dateFormater.dateFormat = "MM-dd HH:mm"
                }
                readbleString = dateFormater.string(from: date)
            }
            return readbleString
        case .full:
            let date = Date.init(timeIntervalSince1970: timestampInSeconds)
            let dateFormater = kDateFormatter
            dateFormater.dateFormat = "yyyy-MM-dd HH:mm"
            return dateFormater.string(from: date)
        case .qface:
            let date = Date.init(timeIntervalSince1970: timestampInSeconds)
            let dateFormater = kDateFormatter
            let ca = Calendar.init(identifier: Calendar.Identifier.gregorian)
            if ca.component(.year, from: Date.init()) != ca.component(.year, from: date) {
                dateFormater.dateFormat = "yyyy年MM月dd日"
            } else {
                dateFormater.dateFormat = "MM月dd日 HH:mm"
            }
            return dateFormater.string(from: date)
        }
    }
    
    /// 播放时间
    public var playTime:String {
        let date = Date.init(timeIntervalSince1970: v)
        if v > 3600.0 {
            let df = DateFormatter.init()
            df.dateFormat = "HH:mm:ss"
            return df.string(from: date)
        }
        let df = kDateFormatter
        df.dateFormat = "mm:ss"
        return df.string(from: date)
    }
}


public extension CMExt where DT == Date {
    /// 返回简短日期字符串yyyy-MM-dd
    public var short:String {
        let dateFormater = kDateFormatter
        dateFormater.dateFormat = "yyyy-MM-dd"
        let dateString = dateFormater.string(from: v)
        return dateString
    }
}
